package reduce.fz.com.reduce.custom;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import org.apmem.tools.layouts.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.Tag;

/**
 * 流式布局的tabList
 */
public class TagListView extends FlowLayout implements OnClickListener {

    protected boolean mIsDeleteMode;
    protected OnTagCheckedChangedListener mOnTagCheckedChangedListener;
    protected OnTagClickListener mOnTagClickListener;
    protected int mTagViewBackgroundResId;
    protected int mTagViewTextColorResId;
    protected final List<Tag> mTags = new ArrayList<Tag>();
    protected Resources mResources;

    /**
     * @param context
     */
    public TagListView(Context context) {
        super(context);
        init(context);
    }

    /**
     * @param context
     * @param attributeSet
     */
    public TagListView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context);
    }

    /**
     * @param context
     * @param attributeSet
     * @param defStyle
     */
    public TagListView(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        init(context);
    }

    @Override
    public void onClick(View v) {
        if ((v instanceof TagView)) {
            Tag localTag = (Tag) v.getTag();
            if (this.mOnTagClickListener != null) {
                this.mOnTagClickListener.onTagClick((TagView) v, localTag);
            }
        }
    }

    private void init(Context context) {
        mResources = context.getResources();
    }

    /**
     * 设置tagList
     *
     * @param tagList
     */
    public void setTags(List<? extends Tag> tagList) {
        setTags(tagList, false);
    }

    /**
     * 设置tagList
     *
     * @param tagList
     * @param checkEnable
     */
    public void setTags(List<? extends Tag> tagList, boolean checkEnable) {
        removeAllViews();
        mTags.clear();
        for (int i = 0; i < tagList.size(); i++) {
            addTag( tagList.get(i), checkEnable);
        }
    }

    /**
     * 添加tagList
     *
     * @param tagList
     */
    public void addTags(List<Tag> tagList) {
        addTags(tagList, false);
    }

    /**
     * 添加tag
     *
     * @param tagList
     * @param checkEnable
     */
    public void addTags(List<Tag> tagList, boolean checkEnable) {
        for (int i = 0; i < tagList.size(); i++) {
            addTag((Tag) tagList.get(i), checkEnable);
        }
    }

    /**
     * 添加tag
     *
     * @param tagId
     * @param title
     */
    public void addTag(Long tagId, String title) {
        addTag(tagId, title, false);
    }

    /**
     * 添加tag
     *
     * @param tagId
     * @param title
     * @param checkEnable
     */
    public void addTag(Long tagId, String title, boolean checkEnable) {
        addTag(new Tag(tagId, title), checkEnable);
    }

    /**
     * 添加tag
     *
     * @param tag
     */
    public void addTag(Tag tag) {
        addTag(tag, false);
    }

    /**
     * 添加tag
     *
     * @param tag
     * @param checkEnable
     */
    public void addTag(Tag tag, boolean checkEnable) {
        mTags.add(tag);
        inflateTagView(tag, checkEnable);
    }

    protected int mTagViewResId = R.layout.view_tag;

    /**
     * 指定tagViewList加载的tagView布局
     * @param tagViewResId
     */
    public void setTagViewTesId(int tagViewResId){
        mTagViewResId = tagViewResId;
    }
    /**
     * 初始化tagView
     *
     * @param tag
     * @param checkEnable
     */
    protected void inflateTagView(final Tag tag, boolean checkEnable) {

        TagView localTagView = (TagView) View.inflate(getContext(),
                mTagViewResId, null);
        localTagView.setText(tag.bname);
        localTagView.setTag(tag);

        if (mTagViewTextColorResId > 0) {
//            mTagViewTextColorResId = getResources().getColor(R.color.blue);
//        } else {
            localTagView.setTextColor(mTagViewTextColorResId);
        }


//        if (mTagViewBackgroundResId <= 0) {
//            mTagViewBackgroundResId = R.drawable.tag_bg;
//        }
        if( mTagViewBackgroundResId > 0 ) {
            localTagView.setBackgroundResource(mTagViewBackgroundResId);
        }

        localTagView.setChecked(tag.isChecked);
        localTagView.setCheckEnable(checkEnable);
        if (mIsDeleteMode) {
            int k = (int) TypedValue.applyDimension(1, 5.0F, getContext()
                    .getResources().getDisplayMetrics());
            localTagView.setPadding(localTagView.getPaddingLeft(),
                    localTagView.getPaddingTop(), k,
                    localTagView.getPaddingBottom());
            localTagView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                    R.drawable.forum_tag_close, 0);
        }
        if (tag.backgroundResId > 0) {
            localTagView.setBackgroundResource(tag.backgroundResId);
        }
        if ((tag.leftDrawableResId > 0) || (tag.rightDrawableResId > 0)) {
            localTagView.setCompoundDrawablesWithIntrinsicBounds(
                    tag.leftDrawableResId, 0, tag.rightDrawableResId, 0);
        }
        localTagView.setOnClickListener(this);
        localTagView
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    public void onCheckedChanged(
                            CompoundButton paramAnonymousCompoundButton,
                            boolean paramAnonymousBoolean) {
                        tag.isChecked = paramAnonymousBoolean;
                        if (TagListView.this.mOnTagCheckedChangedListener != null) {
                            TagListView.this.mOnTagCheckedChangedListener
                                    .onTagCheckedChanged(
                                            (TagView) paramAnonymousCompoundButton,
                                            tag);
                        }
                    }
                });
        FlowLayout.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.leftMargin = mResources.getDimensionPixelOffset(R.dimen.dimen_9);
        lp.rightMargin = mResources.getDimensionPixelOffset(R.dimen.dimen_9);
        lp.topMargin = mResources.getDimensionPixelOffset(R.dimen.dimen_12);
        lp.bottomMargin = mResources.getDimensionPixelOffset(R.dimen.dimen_12);
        addView(localTagView, lp);
    }


    public List<Tag> getTags() {
        return mTags;
    }

    public View getViewByTag(Tag tag) {
        return findViewWithTag(tag);
    }

    public void removeTag(Tag tag) {
        mTags.remove(tag);
        removeView(getViewByTag(tag));
    }

    public void setDeleteMode(boolean deleteMode) {
        mIsDeleteMode = deleteMode;
    }

    public void setOnTagCheckedChangedListener(
            OnTagCheckedChangedListener onTagCheckedChangedListener) {
        mOnTagCheckedChangedListener = onTagCheckedChangedListener;
    }

    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        mOnTagClickListener = onTagClickListener;
    }

    public void setTagViewBackgroundRes(int res) {
        mTagViewBackgroundResId = res;
    }

    public void setTagViewTextColorRes(int res) {
        mTagViewTextColorResId = res;
    }

    public static abstract interface OnTagCheckedChangedListener {
        public abstract void onTagCheckedChanged(TagView tagView, Tag tag);
    }

    public static abstract interface OnTagClickListener {
        public abstract void onTagClick(TagView tagView, Tag tag);
    }

}

